package com.zhipu.balance.pop.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.TimeUtils
import com.zhipu.balance.databinding.ItemNumberPopupBinding
import com.zhipu.balance.listener.AddNumberOnClickListener
import com.zhipu.balance.mobile.event.*

class NumberAdapter : RecyclerView.Adapter<NumberAdapter.ItemViewHolder>() {

    /**
     * 操作类别
     */
    private var stringDetectionName: String? = null
    fun setStringDetectionName(stringDetectionName: String?) {
        this.stringDetectionName = stringDetectionName
    }

    /**
     * 稻谷
     */
    private lateinit var mRecordRiceList : List<RecordRice>
    fun setRecordRice(mRecordRice : List<RecordRice>){
        this.mRecordRiceList = mRecordRice
        notifyDataSetChanged()
    }

    /**
     * 小麦
     */
    private lateinit var mRecordWheatList : List<RecordWheat>
    fun setRecordWheat(mRecordWheat : List<RecordWheat>){
        this.mRecordWheatList = mRecordWheat
        notifyDataSetChanged()
    }

    /**
     * 记录玉米
     */
    private lateinit var mRecordCornList : List<RecordCorn>
    fun setRecordCorn(mRecordCornList : List<RecordCorn>){
        this.mRecordCornList = mRecordCornList
        notifyDataSetChanged()
    }
    /**
     * 记录大米
     */
    private lateinit var mRecordDaMiList : List<RecordDaMi>
    fun setRecordDaMi(mRecordDaMiList : List<RecordDaMi>){
        this.mRecordDaMiList = mRecordDaMiList
        notifyDataSetChanged()
    }
    /**
     * 记录大豆
     */
    private lateinit var mRecordSoybeanList : List<RecordSoybean>
    fun setRecordSoybean(mRecordSoybeanList : List<RecordSoybean>){
        this.mRecordSoybeanList = mRecordSoybeanList
        notifyDataSetChanged()
    }
    /**
     * 记录油菜籽
     */
    private lateinit var mRecordRapeseedList : List<RecordRapeseed>
    fun setRecordRapeseed(mRecordRapeseedList : List<RecordRapeseed>){
        this.mRecordRapeseedList = mRecordRapeseedList
        notifyDataSetChanged()
    }

    /**
     * 点击
     */
    private lateinit var mAddNumberOnClickListener : AddNumberOnClickListener
    fun setAddNumberOnClickListener(mAddNumberOnClickListener : AddNumberOnClickListener){
        this.mAddNumberOnClickListener = mAddNumberOnClickListener
    }



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val binding = ItemNumberPopupBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemViewHolder(binding)
    }


    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        when(stringDetectionName){
            "稻谷" ->{
                var mRecordRice = mRecordRiceList[position]
                //唯一识别码
                holder.binding.RiceNumber.text = mRecordRice.uniqueId
                //平均值
                if(mRecordRice.impurityAverageValue
                    || mRecordRice.roughnessRateAverageValue
                    || mRecordRice.milledRiceAverageValue
                    || mRecordRice.ExternalValleyRoughnessAverageValue
                    || mRecordRice.YellowGrainRiceAverageValue
                    || mRecordRice.intermixingAverageValue
                    || mRecordRice.RiceOutputAverageValue){
                    holder.binding.AverageValue.text = "已计算"
                }else{
                    holder.binding.AverageValue.text = "未计算"
                }
                //时间
                holder.binding.inspectionDate.text =  TimeUtils.millis2String(mRecordRice.inspectionDate)
                //点击
                holder.binding.Item.setOnClickListener {
                    mAddNumberOnClickListener.onSkipClicked(position,mRecordRice)
                }
            }
            "小麦" ->{
                var mRecordWheat = mRecordWheatList[position]
                //唯一识别码
                holder.binding.RiceNumber.text = mRecordWheat.uniqueId
                //平均值
                if(mRecordWheat.impurityAverageValue
                    || mRecordWheat.imperfectGrainValue
                    || mRecordWheat.glutenSampleValue
                    || mRecordWheat.densityAverageValue){
                    holder.binding.AverageValue.text = "已计算"
                }else{
                    holder.binding.AverageValue.text = "未计算"
                }
                //时间
                holder.binding.inspectionDate.text =  TimeUtils.millis2String(mRecordWheat.inspectionDate)
                //点击
                holder.binding.Item.setOnClickListener {
                    mAddNumberOnClickListener.onSkipClicked(position,mRecordWheat)
                }
            }
            "玉米" ->{
                var mRecordCorn = mRecordCornList[position]
                //唯一识别码
                holder.binding.RiceNumber.text = mRecordCorn.uniqueId
                //平均值
                if(mRecordCorn.impurityAverageValue
                    || mRecordCorn.imperfectGrainValue
                    || mRecordCorn.densityAverageValue){
                    holder.binding.AverageValue.text = "已计算"
                }else{
                    holder.binding.AverageValue.text = "未计算"
                }
                //时间
                holder.binding.inspectionDate.text =  TimeUtils.millis2String(mRecordCorn.inspectionDate)
                //点击
                holder.binding.Item.setOnClickListener {
                    mAddNumberOnClickListener.onSkipClicked(position,mRecordCorn)
                }
            }
            "大米" ->{
                var mRecordDaMi = mRecordDaMiList[position]
                //唯一识别码
                holder.binding.RiceNumber.text = mRecordDaMi.uniqueId
                //平均值
                if(mRecordDaMi.impurityAverageValue
                    || mRecordDaMi.imperfectGrainValue
                    || mRecordDaMi.qualityOfBrokenValue
                    || mRecordDaMi.yellowGrainRiceSampleValue
                    || mRecordDaMi.massOfMixedSampleValue){
                    holder.binding.AverageValue.text = "已计算"
                }else{
                    holder.binding.AverageValue.text = "未计算"
                }
                //时间
                holder.binding.inspectionDate.text =  TimeUtils.millis2String(mRecordDaMi.inspectionDate)
                //点击
                holder.binding.Item.setOnClickListener {
                    mAddNumberOnClickListener.onSkipClicked(position,mRecordDaMi)
                }
            }
            "大豆" ->{
                var mRecordSoybean = mRecordSoybeanList[position]
                //唯一识别码
                holder.binding.RiceNumber.text = mRecordSoybean.uniqueId
                //平均值
                if(mRecordSoybean.impurityAverageValue
                    || mRecordSoybean.thermalDamageValue
                    || mRecordSoybean.damagedGrainValue
                    || mRecordSoybean.immatureBrokenGrainValue){
                    holder.binding.AverageValue.text = "已计算"
                }else{
                    holder.binding.AverageValue.text = "未计算"
                }
                //时间
                holder.binding.inspectionDate.text =  TimeUtils.millis2String(mRecordSoybean.inspectionDate)
                //点击
                holder.binding.Item.setOnClickListener {
                    mAddNumberOnClickListener.onSkipClicked(position,mRecordSoybean)
                }
            }
            "油菜籽" ->{
                var mRecordRapeseed = mRecordRapeseedList[position]
                //唯一识别码
                holder.binding.RiceNumber.text = mRecordRapeseed.uniqueId
                //平均值
                if(mRecordRapeseed.impurityAverageValue
                    || mRecordRapeseed.sproutWeightValue
                    || mRecordRapeseed.moldWeightValue
                    || mRecordRapeseed.thermalDamageValue
                    || mRecordRapeseed.immatureGrainValue){
                    holder.binding.AverageValue.text = "已计算"
                }else{
                    holder.binding.AverageValue.text = "未计算"
                }
                //时间
                holder.binding.inspectionDate.text =  TimeUtils.millis2String(mRecordRapeseed.inspectionDate)
                //点击
                holder.binding.Item.setOnClickListener {
                    mAddNumberOnClickListener.onSkipClicked(position,mRecordRapeseed)
                }
            }
        }
    }

    override fun getItemCount(): Int {
        when(stringDetectionName){
            "稻谷" ->{
                return mRecordRiceList.size
            }
            "小麦" ->{
                return mRecordWheatList.size
            }
            "玉米" ->{
                return mRecordCornList.size
            }
            "大米" ->{
                return mRecordDaMiList.size
            }
            "大豆" ->{
                return mRecordSoybeanList.size
            }
            "油菜籽" ->{
                return mRecordRapeseedList.size
            }
        }
        return 0
    }



    class ItemViewHolder(var binding: ItemNumberPopupBinding) : RecyclerView.ViewHolder(binding.root) {

    }
}